<template>
    <view class="wai">
        <!--        <view class="box-phone">
            <view class="">手机号</view>
            <input class="input" placeholder="请输入手机号" type="text" />
        </view>
        <view class="box-phone">
            <view class="">验证码</view>
            <input class="input" placeholder="请输入验证码" type="text" />
            <view class="code" @click="getVerificationCode">{{ isCounting ? countdownText : '获取验证码' }}</view>
        </view> -->
        <view class="box-phone">
            <view class="">新密码</view>
            <input v-model="mima" class="input" placeholder="请输入六位数新密码" type="password" />
        </view>
        <view class="box-phone">
            <view class="">确认密码</view>
            <input v-model="xinmima" class="input2" placeholder="请再次输入六位数新密码" type="password" />
        </view>
        <!-- 按钮 -->
        <view class="btn" @click="btnClack">确定</view>
    </view>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from 'vue';
import { updatePayPassword } from '@/api/index.js';
import { utils } from '@/utils/utils.js';
const countdown = ref(60); //倒计时
const isCounting = ref(false); //多次点击只执行一次,控制显示秒数
let timer = null;
//
const mima = ref(''); //密码
const xinmima = ref(''); //确认密码
// 确认密码
const btnClack = async () => {
    const parms = ref({
        password: mima.value,
        repassword: xinmima.value
    });
    if (mima.value.length != 6 || xinmima.value.length != 6) return utils('请输入六位数支付密码');
    if (mima.value !== xinmima.value) return utils('两次密码不一致');
    const ret = await updatePayPassword(parms.value);
    if (ret.code == 1) {
        uni.navigateBack();
        utils(ret.msg);
    } else {
        utils(ret.msg);
    }
};

// 计算倒计时的文本
const countdownText = computed(() => {
    if (isCounting.value) {
        return `${countdown.value}s重新获取`;
    }
});
// 点击获取验证码
const getVerificationCode = async () => {
    if (isCounting.value) return; // 如果已经在倒计时，则不执行
    isCounting.value = true; // 开始倒计时
    startCountdown();
};
// 倒计时函数
const startCountdown = () => {
    timer = setInterval(() => {
        if (countdown.value > 1) {
            //如果秒数大于零就--
            countdown.value--;
        } else {
            //如果不大于零就移除定时器,其他数据复原
            clearInterval(timer);
            isCounting.value = false; //结束倒计时
            countdown.value = 60; //秒数复原
        }
    }, 1000);
};
// 移除定时器
const resetCountdown = () => {
    clearInterval(timer);
    isCounting.value = false;
    countdown.value = 60;
};
onUnmounted(() => {
    // 组件卸载时清除定时器
    resetCountdown();
});
</script>

<style lang="scss" scoped>
.wai {
    .btn {
        background-color: #f8bf5a;
        width: 100%;
        height: 90rpx;
        text-align: center;
        line-height: 90rpx;
        color: #fff;
        border-radius: 50rpx;
        margin-top: 60rpx;
    }
    .box-phone {
        .code {
            border-radius: 50rpx;
            color: #fff;
            background-color: #34d19d;
            padding: 20rpx;
            font-size: 30rpx;
        }
        .input2 {
            flex: 1;
            margin-left: 48rpx;
        }
        .input {
            flex: 1;
            margin-left: 76rpx;
        }
        height: 120rpx;
        border-bottom: 2rpx solid #f7f7f7;
        display: flex;
        align-items: center;
    }
    padding: 0 30rpx;
}
</style>
